<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>应急信息发布明细查询</title>
    <link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">
    <link href="../../css/style.css" rel="stylesheet" type="text/css">
    <link href="../../css/jquery-ui-1.10.4.custom.min.css" rel="stylesheet" type="text/css">
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.8.3.js"></script>
    <script src="../../js/util.js"></script>
    <script src="../../js/jquery-ui-1.10.4.custom.min.js"></script>
    <style>
        #ui-id-1 {
            overflow-y: scroll;
            max-height: 200px;
        }
    </style>
    <script type="text/javascript">

        layui.use(['laypage', 'layer', 'table', 'form', 'jquery', 'laydate', 'element'], function () {
            var table = layui.table;
            var layer = layui.layer;
            var form = layui.form;
            var $ = layui.$;
            var laydate = layui.laydate;
            var index;
            var mgrNameArr=[];
            var mgrIdArr=[];
            var tableData = {
                elem: "#table",//表格id
                id: "modelReload", //id
                url: '/smsReportDetail/find',
                method: 'post',
                request: {
                    pageName: 'page',
                    limitName: 'limit'
                },
                response: {
                    statusName: 'code',
                    statusCode: 0,
                    msgName: 'msg',
                    countName: 'count',
                    dataName: 'data'
                },
                page: true,
                count: "count",
                limit: 10,
                limits: [10, 20, 30, 40, 50],
                layout: ['prev', 'page', 'next'],
                prev: "上一页",
                next: "下一页",
                cols: [[ //表头
                      {field: 'cityName', title: '地市',  align: 'center'}
                    , {field: 'countyName', title: '区县', align: 'center'}
                    , {field: 'mgrName', title: '项目名称',align: 'center'}
                    , {field: 'mgrStateName', title: '项目状态',  align: 'center'}
                   /*  , {field: 'regionId', title: '区域', width: '9%', align: 'center'}
                    , {field: 'stationName', title: '基站', width: '9%', align: 'center'} */
                    , {field: 'msisdn', title: '用户号码',  align: 'center'}
                    , {field: 'userPlaceName', title: '用户归属省', align: 'center'}
                    , {field: 'userCityName', title: '用户归属地市', align: 'center'}
                    , {field: 'sendTime', title: '发送时间',  align: 'center'}
                    , {field: 'sendStateName', title: '发送状态', align: 'center'}
                    , {field: 'content', title: '内容', align: 'center'}
                ]], done: function (res) {
                    layer.close(index);
                }
            };
    
    		$("#mgrName").autocomplete({
    			source : function(request, response) {
    				var key = $("#mgrName").val();
    				var cityId = $("#cityId").val();
    				var countyId = $("#countyId").val();
    				var url = '/util/selectMgrNameLike';
    				var param = {
    					cityId : cityId,
    					countyId : countyId,
    					key : key
    				};
    				$.ajax({
    					type : 'post',
    					async : false,
    					url : url,
    					data : param,
    					success : function(json) {
    						var data = json.data;
    						source = [];
    						for (var i = 0; i < data.length; i++) {
    							source[i] = data[i].name;
    						}
    						console.log(source);
    						response(source);
    					},
    					error : function() {
    						layer.msg('系统异常，请联系管理员处理!');
    					}
    				}
    				);
    			},
    			messages : { //message设置不出现匹配关键字的结果
    				noResults : '',
    				results : function() {}
    			}
    		});
    
    		//根据项目id查询区域
    		function findRegionInfo(mgrId) {
                var data;
                var url = '/util/findRegionInfo';
                var param = {
                    mgrId: mgrId,
                    cityId: $("#cityId").val()
                };
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        data: param,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('findRegionInfo系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }

            //根据区域查询基站
            function findStationInfo(regionId) {
                var data;
                var url = '/util/findStationInfo';
                var param = {
                    cityId: $("#cityId").val(),
                    regionId: regionId,
                    mgrId: $("#mgrId").val()
                };
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        data: param,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('findStationInfo系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }

            


            //导出
            $("#download").on('click', function () {
                var mgrId = $("#mgrId").val();
                var mgrName = $("#mgrName").val();
                var cityId = $("#cityId").val();
                var countyId = $("#countyId").val();
                var startTime = $("#startTime").val();
                var endTime = $("#endTime").val();
                var way = $('input:radio:checked').val();
                var month = $("#yearmonth").val();
                if (cityId == '') {
                    layer.msg('您必须选择一个地市');
                } else if (countyId == '') {
                    layer.msg('您必须选择一个区县');
                } else if (mgrId == ''&&mgrName == '') {
                    layer.msg('你必须选择一个项目或输入项目简称');
                } else if (!judetmentMonth(startTime, endTime) && way == 0) {
                    layer.msg('您选择的时间段已跨月,请重新选择');
                } else if (way == 1 && month == "") {
                    layer.msg('您必须选择一个月份');
                } else if(mgrName!=""&&IsInArray(mgrNameArr,mgrName)==false){
                     layer.msg('该项目名称不存在或者您没有权限导出该项目');
                }else {
                     if(mgrId==''&&mgrName!=''){
                        mgrId=mgrIdArr[mgrNameArr.indexOf(mgrName)];
                    }
                    //var regionId = $("#regionId").val();
                    //var stationId = $("#stationId").val();
                    var regionId = "";
                    var stationId = "";
                    url = '/smsReportDetail/download?mgrId=' + mgrId
                        + '&mgrName=' + mgrName
                        + '&cityId=' + cityId
                        + '&countyId=' + countyId
                        + '&startTime=' + startTime
                        + '&endTime=' + endTime
                        + '&way=' + way
                        + '&month=' + month
                        + '&regionId=' + regionId
                        + '&stationId=' + stationId;
                    location.href = url;
                }
            });
 
            //监听区县
            form.on('select(cityId)', function (data) {
                var data2 = selectCountyId(data.value);
                appendCounty(data2);
                var data3 = selectMgrInfo(data.value);
                 appendMgr(data3);
            });
            //根据城市ID查询区县
            function selectCountyId(cityId) {
                var data;
                var url = '/util/findCountyByCityId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //动态拼接区县
            function appendCounty(data){
                if (data.length != null) {
                    var options = '<option value="">请先选择地市</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].countyId + '">' + data[i].countyName + '</option>';
                    }
                    $('#countyId').html(options);
                } else {
                    $('#countyId').html('该地市下没有区县');
                }
                form.render();
            }
            
            //监听项目
            form.on('select(countyId)', function (data) {
                var data1 = selectMgrInfo(data.value);
                appendMgr(data1);
            });
            
                     //根据城市ID查询项目
            function selectMgrInfo(cityId) {
                var data;
                var url = '/util/findByCityId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            
            //根据城市ID和区县查询项目
            function selectMgrInfo(countyId) {
                var data;
                var url = '/util/findByMgrId?countyId=' + countyId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //动态拼接项目
            function appendMgr(data) {
                if (data.length != null) {
                    var options = '<option value="">请选择</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
                        mgrNameArr.push(data[i].name);
                        mgrIdArr.push(data[i].id);
                    }
                    $('#mgrId').html(options);
                } else {
                    $('#mgrId').html('<option value="">该区县下没有项目</option>');
                }
                form.render();
            }

            //身份识别
            $(document).ready(function () {
                $.ajax({
                    type: 'post',
                    async: false,
                    url: '/util/findUser',
                    success: function (json) {
                        user=json;
                        if (json.duty == 1) {//地市
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data2 = selectCountyId(json.cityId);
                            appendCounty(data2);
                            var data1 = selectMgrInfo(json.cityId);
                            appendMgr(data1);
                            form.render();
                        } else if (json.duty == 2) {//项目
                            appendMgr(json.list);
                            var data2 = selectCountyId(json.cityId);
                            appendCounty(data2);
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            $("#countyId").val(json.countyId);
                            $("#countyId").attr("disabled", true);
                            form.render();
                        }else if (json.duty == 3||json.duty == 6||json.duty == 7) {//区县
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data = selectMgrInfo(json.countyId);
                            appendMgr(data);
                            var data2 = selectCountyId(json.cityId);
                            appendCounty(data2);
                            $("#countyId").val(json.countyId);
                            $("#countyId").attr("disabled", true);
                            form.render();
                        }else if(json.duty == 4){
                            if(json.cityId==25){//省权限
                            
                            }else if(json.cityId!=25&&checkStr(json.countyId)==""){//地市权限
	                            $("#cityId").val(json.cityId);
	                            $("#cityId").attr("disabled", true);
	                            var data2 = selectCountyId(json.cityId);
	                            appendCounty(data2);
	                            var data1 = selectMgrInfo(json.cityId);
	                            appendMgr(data1);
	                            form.render();
                            }else if(json.cityId!=25&&checkStr(json.countyId)!=""){//区县权限
	                            $("#cityId").val(json.cityId);
	                            $("#cityId").attr("disabled", true);
	                            var data = selectMgrInfo(json.countyId);
	                            appendMgr(data);
	                            var data2 = selectCountyId(json.cityId);
	                            appendCounty(data2);
	                            $("#countyId").val(json.countyId);
	                            $("#countyId").attr("disabled", true);
	                            form.render();
                            }
                        } 
                    }, error: function () {
                        layer.msg('系统异常，请联系管理员处理!');
                    }
                });
            });
            //校验字符串
			function checkStr(str,defaultVal){
				if (typeof(str) == "undefined" || str == null || str == "null") {
					if (defaultVal == undefined) {
						str = "";
					} else {
						str = defaultVal;
					}
				}
				return str;
			}

            //加载数据表
            $('#query').on('click', function () {
                var mgrId = $("#mgrId").val();
                var countyId = $("#countyId").val();
                var mgrName = $("#mgrName").val();
                var cityId = $("#cityId").val();
                var startTime = $("#startTime").val();
                var endTime = $("#endTime").val();
                var way = $('input:radio:checked').val();
                var month = $("#yearmonth").val();
                if (cityId == ''&&mgrName=='') {
                    layer.msg('您必须选择一个地市');
                } /* else if (countyId == ''&&mgrName=='') {
                    layer.msg('你必须选择一个区县');
                } */ else if (mgrId == ''&&mgrName=='') {
                    layer.msg('您必须选择一个项目或输入项目简称');
                } else if (!judetmentMonth(startTime, endTime) && way == 0) {
                    layer.msg('对不起，您选择的时间段跨月了，请重新选择');
                } else if (way == 1 && month == "") {
                    layer.msg('您必须选择一个月份');
                } else if (mgrId != ''&&mgrName !='') {
                    layer.msg('您只能选择项目或项目简称中的一个查询');
                }/* else if(mgrName!=""&&IsInArray(mgrNameArr,mgrName)==false){
                     layer.msg('该项目名称不存在或者您没有权限查看该项目');
                } */
                else {
                    index = layer.load(1, {
                        shade: [0.1, '#fff'] //0.1透明度的白色背景
                    });
                    table.reload('modelReload', {
                        method: 'post',
                        page: {
                            curr: 1
                        },
                        where: {
                            cityId: $("#cityId").val(),
                            countyId: $("#countyId").val(),
                            way: $('input:radio:checked').val(),
                            startTime: $("#startTime").val(),
                            endTime: $("#endTime").val(),
                            month: $("#yearmonth").val(),
                            mgrId: $("#mgrId").val(),
                            mgrName: $("#mgrName").val(),
                          /*   regionId: $("#regionId").val(),
                            stationId: $("#stationId").val() */
                            /* regionId:"",stationId:"" */
                            
                        }
                    });
                }
            });
            //加载区域
            form.on('select(mgrId)', function (data) {

                var data = findRegionInfo(data.value);
                if (data.length != null) {
                    var options = '<option value="">请选择</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].regionId + '">' + data[i].regionName + '</option>';
                    }
                    $('#regionId').html(options);
                    $('#stationId').html('<option value="">请先选择区域</option>');
                } else {
                    $('#regionId').html('<option value="">该项目下没有区域信息</option>');
                }
                form.render();
            });
            //加载基站
            form.on('select(regionId)', function (data) {
                var data = findStationInfo(data.value);
                if (data.length != null) {
                    var options = '<option value="">请选择</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].stationId + '">' + data[i].stationName + '</option>';
                    }
                    $('#stationId').html(options);
                } else {
                    $('#stationId').html('<option value="">该区域下没有基站信息</option>');
                }
                form.render();
            });
            //时间选择器的样式
            //获取系统时间，做时间选择
            //获取指定月的天数
            var myDate = new Date();
            //当前年份
            var year = myDate.getFullYear();
            //当前月份
            var maxmonth = myDate.getMonth() + 1;
            maxmonth = (maxmonth >= 10 ? maxmonth : 0 + "" + maxmonth);
            //获取当前日期
            var date = myDate.getDate();
            //获取当前小时
            var hour = myDate.getHours();
            //获取当前分钟
            var min = myDate.getMinutes();
            //当前时间
            var loaclTime = year + '-' + maxmonth + "-" + date + " " + hour + ":" + min + ":" + "00";

            var nowMonth = year+maxmonth;
            //最大查询的月份
            var maxdate = year + '-' + myDate.getMonth()+1 + '-' + date;

            var minmonth = maxmonth - 5;
            var minyear = (minmonth > 0 ? year : year - 1);
            minmonth = (minmonth > 0 ? minmonth : minmonth + 12);
            //查询的最小月份
            var mindate = minyear + '-' + minmonth + '-' + date;


            //查询的时间段上限
            var maxtime = loaclTime;
            //查询的时间段下限
            var mintime = minyear + '-' + minmonth + "-01" + " 00:00:00";

            //初始化按时间段查询结束时间
            var initiaMaxTime = loaclTime;
            //初始化按时间段查询开始时间
            var initiaMinTime = year + '-' + maxmonth + "-" + date + " 00:00:00";
            //上个月的时间
            var mm = (myDate.getMonth() > 10 ? myDate.getMonth() : 0 + "" + myDate.getMonth());
            mm = (myDate.getMonth() == 0 ? 12 : mm);
            var initMonth = (myDate.getMonth() == 0 ? year - 1 : year) + "" + mm;

            laydate.render({
                elem: '#yearmonth'
                , type: 'month'
                , format: 'yyyyMM'
                , btns: ['clear', 'confirm']
                , trigger: 'click'
                , value: nowMonth
                , max: maxdate
                , min: mindate
            });
            laydate.render({
                elem: '#startTime'
                , type: 'datetime'
                , format: 'yyyy-MM-dd HH:mm:ss'
                , btns: ['clear', 'confirm']
                , trigger: 'click'
                , value: initiaMinTime
                , max: maxtime
                , min: mintime
            });
            laydate.render({
                elem: '#endTime'
                , type: 'datetime'
                , format: 'yyyy-MM-dd HH:mm:ss'
                , trigger: 'click'
                , btns: ['clear', 'confirm']
                , value: initiaMaxTime
                , max: maxtime
                , min: mintime
            });
            table.render(tableData);
        });
    </script>
</head>
<body>
<!--nav-->
<div class="layui-html-limit">
    <div class="layui-content-body">
        <div class="scroll-box">
            <div class="breadcrumb-box">
                <span class="layui-breadcrumb" lay-separator="|">
                  <a href="javascript:returnIndex2()">首页&nbsp;</a>
                  <a href="#">&nbsp;报表管理&nbsp;</a>
                  <a href="#">&nbsp;短信发送明细查询&nbsp;</a>
                </span>
            </div>
            <div class="content-box">
                <div class="form-select-box">
                    <div class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">省份</label>
                                <div class="layui-input-block" style="width:150px">
                                    <input type="text" name="username" lay-verify="required" value="江苏"
                                           autocomplete="off" class="layui-input"  disabled>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">地市</label>
                                <div class="layui-input-block" style="width:150px">
                                    <select name="cityId" lay-filter="cityId" id="cityId">
                                        <option value="">请选择</option>
                                        <option value="11">苏州</option>
                                        <option value="12">淮安</option>
                                        <option value="13">宿迁</option>
                                        <option value="14">南京</option>
                                        <option value="15">连云港</option>
                                        <option value="16">徐州</option>
                                        <option value="17">常州</option>
                                        <option value="18">镇江</option>
                                        <option value="19">无锡</option>
                                        <option value="20">南通</option>
                                        <option value="21">泰州</option>
                                        <option value="22">盐城</option>
                                        <option value="23">扬州</option>
                                        <option value="25">省级</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">区县</label>
                                <div class="layui-input-block" style="width:150px">
                                    <select name="mgrcountyId" lay-filter="countyId" id="countyId">
                                        <option value="">请先选择地市</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">项目名称</label>
                                <div class="layui-input-block">
                                    <select name="interest" lay-filter="mgrId" id="mgrId">
                                        <option value="">请先选择区县</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                 <label class="layui-form-label">发送时间</label>
                                 <div class="layui-inline">
                                    <input type="radio" value="1" title="按月" name="way">
                                </div> 
                                <div class="layui-inline">
                                    <div class="layui-input-inline" style="width: 100px;">
                                        <input type="text" id="yearmonth" placeholder="请选择月份" class="layui-input"
                                               readonly>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <input type="radio" value="0" title="发送时间段" name="way" checked="">
                                </div>
                                <div class="layui-inline">
                                    <div class="layui-inline">
                                        <div class="layui-input-inline" style="width: 150px;">
                                            <input type="text" name="date" id="startTime" autocomplete="off"
                                                   class="layui-input" >
                                        </div>
                                        <div class="layui-form-mid">-</div>
                                        <div class="layui-input-inline" style="width: 150px;">
                                            <input type="text" name="date" id="endTime" autocomplete="off"
                                                   class="layui-input" >
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-inline" style="color: red;display:none">1）短信明细数据只保存6个月,并且不能跨月查询,当前月份不支持按月查询
                                    </br>2） 导出只可以导出2天以前的数据（原因：短信网关48小时内才完全返回短信是否发送成功）
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            
                           <div class="layui-inline" id="shortname">
                                <label class="layui-form-label">项目简称</label>
                                <div class="layui-input-block">
                                    <input type="text"  name="mgrName" id="mgrName" lay-verify="required" value=""
                                           autocomplete="off" class="layui-input ui-autocomplete-input" />
                                </div>
                            </div> 
                           <!--   <div class="layui-inline">
                                <label class="layui-form-label">区域选择</label>
                                <div class="layui-input-block">
                                    <select lay-filter="regionId" name="regionId" id="regionId">
                                        <option value="">请先选择项目</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">基站</label>
                                <div class="layui-input-block">
                                    <select lay-filter="stationId" name="stationId" id="stationId">
                                        <option value="">请先选择区域</option>
                                    </select>
                                </div>
                            </div>-->
                            <div class="layui-inline">
                                <button class="layui-btn layui-btn-normal" id="query" type="button">查询</button>
                                <!-- <button class="layui-btn layui-btn-normal" id="download">导出</button> -->
                            </div>
                        </div>
                    </div>
                </div>

                <!--表格-->
                <div class="table-box">
                    <table class="layui-table" id="table">
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
</body>
</html>